﻿#pragma once
#include "Protocol.h"
#include "MsgDefines.h"
#include "DownwardProtocol.h"
#include "UpwardProtocol.h"
#include "BinaryUpwardProtocol.h"
#include <QtCore/QProcessEnvironment>
#include <QtCore/QFile>
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <glog/logging.h>
#include <hadoop/SerialUtils.hh>
namespace HadoopPipes {
/**
 * 正常的消息流程:
 * AUTHENTICATION_REQ..START_MESSAGE..SET_JOB_CONF RUN_MAP MAP_ITEM CLOSE
 * AUTHENTICATION_REQ..START_MESSAGE..SET_JOB_CONF RUN_REDUCE REDUCE_KEY/REDUCE_VALUE CLOSE
 */
class BinaryProtocol: public Protocol {
  private:
    HadoopUtils::FileInStream* downStream;
    DownwardProtocol* handler;
    BinaryUpwardProtocol * uplink;
    std::string key;
    std::string value;
    std::string password;
    bool authDone;
    void getPassword(std::string &password);
	void verifyDigestAndRespond(std::string& digest, std::string& challenge) ;
    

    bool verifyDigest(std::string &password, std::string& digest, std::string& challenge) {
      std::string expectedDigest = createDigest(password, challenge);
      if (digest == expectedDigest) {
        return true;
      } else {
        return false;
      }
    }
	std::string createDigest(std::string &password, std::string& msg);
    

  public:
	  BinaryProtocol(QIODevice* down, DownwardProtocol* _handler, QIODevice* up);
    

    UpwardProtocol* getUplink() {
      return uplink;
    }

	virtual void nextEvent() ;
    
	virtual ~BinaryProtocol();
    
  };
}